package Model;

import java.awt.List;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import javax.swing.DefaultListModel;

/**
 * Model
 * ContactsList manage the listing of all the contacts
 * @author Johanna
 *
 */
public class ContactsList {

	private ArrayList<Contact> listing;

	/** Constructor */
	public ContactsList() {
		this.listing = new ArrayList<Contact>();
	}

	/**
	 * To load the CSV file
	 */
	public void readCsv() throws IOException {  

		String csvFileToRead = "adressBook.csv";  
		BufferedReader br = null;  
		String line = "";  
		String splitBy = ";";

		try {  
			br = new BufferedReader(new FileReader(csvFileToRead));  

			br.readLine();

			while ((line = br.readLine()) != null) {  
				String[] info = line.split(splitBy);  

				/*
				 * To get the 3 fields
				 */
				String fname = info[0];
				String lname = info[1];
				String number = info[2];
				String address = info[3];
				String group = info[4];
				String email = info[5];
				String homepage = info[6];

				/*
				 * To create a new contact
				 */
				Contact newContact = new Contact(fname,lname,number, address, group, email, homepage);

				/*
				 * To add the new Contact to the list
				 */
				this.listing.add(newContact);
			}  
		} 

		catch (FileNotFoundException e) {  
			e.printStackTrace();  
		} 

		catch (IOException e) {  
			e.printStackTrace();  
		} 

		finally {  
			if (br != null) {  
				try {  
					br.close();  
				} catch (IOException e) {  
					e.printStackTrace();  
				}  
			}  
		}  
	}

	/**
	 * To create the list that is going to be display
	 * @return
	 */
	public DefaultListModel createDefaultList() {

		DefaultListModel defaultList = new DefaultListModel();
		String nameContact;
		StringBuilder sb = new StringBuilder();

		// To go through the ArrayList
		for(int i=0; i<this.listing.size(); i++) {

			sb.append( listing.get(i).getFname() ).append( " " ).append( listing.get(i).getLname() );

			nameContact = sb.toString();

			sb.setLength(0);

			// To add the name of the contact to the list
			defaultList.addElement(nameContact);
		}

		return defaultList;
	}

	/**
	 * Method to delete a contact from the listing
	 * @param c
	 */
	public void deleteContact(Contact c)
	{
		Contact c_del = new Contact();

		for ( Contact ptContact : this.listing)
		{			
			if( ( ptContact.getFname() ).equals( (String)(c.getFname()) ) )
			{
				c_del = ptContact;
			}
		}

		if(c_del != null)
			this.listing.remove(c_del);

		else {
			System.out.println("Contact non trouv�...");
		}
	}

	/**
	 * Method to search a contact by his name
	 * @param c
	 * @return
	 */
	public Contact searchContact(String[] c)
	{
		int ok = 0;
		Contact c_found = new Contact();

		for ( Contact ptContact : this.listing)
		{
			ok = 0 ; // To know if the contact exists 

			if( (ptContact.getFname()).equals( (String)(c[0]) ) && (ptContact.getLname()).equals( (String)(c[1]) ) )
			{
				c_found = ptContact;
			}
		}

		return c_found;
	}

	/**
	 * Method to search a contact
	 * and return his index in the ArrayList
	 * @param c
	 * @return
	 */
	public int searchContactIndex(String[] c)
	{
		int index = 0;
		Contact c_found = new Contact();

		for(int i = 0; i < this.listing.size(); ++i) 
		{
			if( (this.listing.get(i).getFname()).equals( (String)(c[0]) ) && (this.listing.get(i).getLname()).equals( (String)(c[1]) ) )
			{
				index = i;
			}
		}

		return index;
	}

	/**
	 * Method to sort by letters the list of contacts
	 * @param letter
	 * @return
	 */
	public ArrayList<Contact> sortByLetter(String letter)
	{
		ArrayList<Contact> list = new ArrayList<Contact>();

		for ( Contact ptContact : this.listing)
		{
			// To compare the first letter of the contact name with the letter in parameter
			if( (ptContact.getFname().substring(0, 1)).equals(letter) )
			{
				list.add(ptContact);
			}
		}

		return list;
	}
	
	/**
	 * Method to sort by groups the contacts
	 * @param group
	 * @return
	 */
	public ArrayList<Contact> sortByGroup(String group)
	{
		ArrayList<Contact> list = new ArrayList<Contact>();
		for ( Contact ptContact : this.listing)
		{
			// To compare if the groups are equal
			if( (ptContact.getGroup()).equals(group) )
			{
				list.add(ptContact);
			}
		}

		return list;
	}

	public ArrayList<Contact> getListing() {
		return this.listing;
	}

	public void setListing(ArrayList<Contact> newList) {
		this.listing = newList;
	}

	/**
	 * To sort by alphabetical order
	 * 
	 * @return
	 */
	public ArrayList<Contact> sortAlphaOrder()
	{	
		ArrayList<Contact> listeContact = new ArrayList<Contact>();
		listeContact = this.listing;

		Collections.sort(listeContact, new Comparator<Contact>() {

			@Override
			public int compare(Contact c1, Contact c2) {

				if ( c1 == null)
				{
					if(c2 != null) 
						return -1;
				}

				if (c2 == null)
					return 1;

				int result= c1.getFname().compareTo(c2.getFname());
				if(result==0)
					result = c1.getLname().compareTo(c2.getLname());
				if(result==0) 
					result = c1.getLname().compareTo(c2.getLname());

				return result;
			}
		});

		return listeContact;
	}

}

